//
//  ZWLNavController.swift
//  QY-Swift
//
//  Created by tsc-zwl on 16/4/19.
//  Copyright © 2016年 tsc-zwl. All rights reserved.
//

import UIKit

class ZWLNavController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

//        let navBar = UINavigationBar.appearance()
//        navBar.isTranslucent = false
        
        // 设置背景
//        bar.barTintColor = ZWLColor(0, 190, 139);
//        bar.translucent = YES;
//        [bar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
//        // 设置标题文字属性
//        NSMutableDictionary *barAttrs = [NSMutableDictionary dictionary];
//        barAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
//        [bar setTitleTextAttributes:barAttrs];
        
        // 这里可以统一设置导航条
        
        
        // 1.取出手势&view
        guard let gesture = interactivePopGestureRecognizer else { return }
        gesture.isEnabled = false
        let gestureView = gesture.view
        // 2.获取所有的target
        let target = (gesture.value(forKey: "_targets") as? [NSObject])?.first
        guard let transition = target?.value(forKey: "_target") else { return }
        let action = Selector(("handleNavigationTransition:"))
        // 3.创建新的手势
        let popGes = UIPanGestureRecognizer()
        popGes.maximumNumberOfTouches = 1
        gestureView?.addGestureRecognizer(popGes)
        popGes.addTarget(transition, action: action)

        
        // 滑动返回
        interactivePopGestureRecognizer?.delegate = self;
    }

    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return childViewControllers.count > 1
    }
    

    /**
     拦截 push 进来的控制器
     
     - parameter viewController: vc
     - parameter animated:       anim
     */
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count >= 1 {
            // 在这里需要设置左上角的返回按钮
            let btn = UIButton()
            btn.setTitle("返回", for: UIControlState())
            btn.sizeToFit()
            btn.setTitleColor(UIColor.red, for: UIControlState())
            btn.addTarget(self, action: #selector(ZWLNavController.back), for: UIControlEvents.touchUpInside)
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
            viewController.hidesBottomBarWhenPushed = true

        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    func back() {
        popViewController(animated: true)
    }

}
